var request = require('./request.js');
var config = require('../config.js');
var app = getApp();


function payByTradeIdAction(tradeId){
  return new Promise((resoleve, reject)=>{
    var url = config.payGoodsUrl;
    var openId = wx.getStorageSync('openId');
    var shopId = app.globalData.shopId;
    var params = {
      tradeId: tradeId,
      openId: openId,
      shopId: shopId
    }
    request.requestPost(url, params)
      .then(data => {
        console.log(data);
        wx.requestPayment(
          {
            'timeStamp': data.timeStamp,
            'nonceStr': data.nonceStr,
            'package': data.package,
            'signType': 'MD5',
            'paySign': data.paySign,
            'success': function (res) {
              resoleve(res);
            },
            'fail': function (res) {
              reject(res);
            },
            'complete': function (res) {
              resoleve(res);
            }
          })
      }).catch(msg => {
        reject(msg);
      });
  })
}


module.exports = {
  payByTradeIdAction: payByTradeIdAction
}